package com.fr.vod.videoshareucp_testprotocole;

import java.net.InetAddress;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.MulticastLock;
import android.os.AsyncTask;
import android.util.Log;
import fr.vod.net.Receiver;
import fr.vod.net.ReceiverMulticast;
import fr.vod.net.Sender;
import fr.vod.net.SenderMulticast;
import fr.vod.util.LogHelper;

public class TransfertTask extends AsyncTask<String, String, String>{

	private Context mContext;
	
	
	static boolean isServer;
	private int port=3000;
	private String ipServer = "224.0.0.2";
	private String ipClient = "224.0.0.1";
	private String id = "01";
	
	private Receiver receiver;
	private Sender sender;
	
	public TransfertTask(Context context){
		this.mContext=context;
		
	}
	
	@Override
	protected void onPreExecute() {
			WifiManager wifi = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
			MulticastLock mLock = wifi.createMulticastLock("mylock");
			mLock.acquire();
	}
	
	@Override
	protected String doInBackground(String... params) {
		String msg = "";
		if(isServer){
			try {
				sender = new SenderMulticast(InetAddress.getByName(ipServer),port);
				receiver = new ReceiverMulticast(InetAddress.getByName(ipServer),port);
			
				while(!msg.startsWith("LENGHT_OK_")){
					sender.send("FILE_LENGHT_XXX");
					LogHelper.information("TransfertTask", "msg envoyé : FILE_LENGHT_XXX");
						
					msg = receiver.receive();
					Log.i("TransfertTask","message recu : "+msg);
				}
				while(!msg.startsWith("CLIENT_READY_")){
					sender.send("SERVER_READY_"+id);
					LogHelper.information("TransfertTask", "msg envoyé : SERVER_READY_");
					msg = receiver.receive();
					Log.i("TransfertTask","message recu : "+msg);
				}
				while(!msg.startsWith("START_")){
						msg = receiver.receive();
						Log.i("TransfertTask","message recu : "+msg);
				}
							//commencer l'envoi du bloc demandé
					
			}
			catch (Exception e) {
				e.printStackTrace();
				LogHelper.information("E", "erreur de MERDER");
			}
		
			
		}
		//alors c'est un client
		else {
			try {
				receiver = new ReceiverMulticast(InetAddress.getByName(ipClient),port);
				sender = new SenderMulticast(InetAddress.getByName(ipClient),port);
				
				while (!msg.startsWith("FILE_LENGHT_")){
					
					msg = receiver.receive();
					Log.i("TransfertTask","message recu : "+msg);
				}
				while(!msg.startsWith("SERVER_READY_")){
					
					//controle sur la longueur du fichier à faire si OK
					sender.send("LENGHT_OK_");
					LogHelper.information("TransfertTask", "msg envoyé : LENGHT_OK");
				}
				while(true){
					msg = receiver.receive();
					Log.i("TransfertTask","message recu : "+msg);
					sender.send("CLIENT_READY_"+id);
					sender.send("START_0");
					LogHelper.information("TransfertTask", "msg envoyé : CLIENT_READY_"+id+"\nSTART_0");
				}
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

}
